/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.gateway.route;

import java.net.URI;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;

import com.alibaba.fastjson.JSONArray;
import com.hyts.infodb.config.SystemConfiguration;
import com.hyts.infodb.error.subclass.GatewayException;
import com.hyts.infodb.gateway.bean.RouteDefinitionVO;
import com.hyts.infodb.gateway.service.GatewayService;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/** 
 * @author LiBo/Alex
 * @version V1.0
 * 网关操作服务维护数据类 
 */
@Slf4j
//@Component
public class DataBaseRouteDefinitionRepository implements RouteDefinitionRepository{
    
    
    @Autowired
    GatewayService gatewayService;
    
    /* (非 Javadoc)
     * @return
     * @see org.springframework.cloud.gateway.route.RouteDefinitionLocator#getRouteDefinitions()  
     * @exception
     */ 
    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        RouteDefinitionVO vo = gatewayService.findAllGatewayDataList();
        return Flux.fromIterable(vo.getContents().stream().map(param->{
            RouteDefinition rd = new RouteDefinition();
            if(Strings.isEmpty(param.getServiceId())) {
                if(Strings.isEmpty(param.getUrl()))
                    throw new GatewayException("路由转换异常，Url和service_id不允许都为空的服务存在");
                rd.setUri(URI.create(param.getUrl()));
            }
            rd.setId(param.getId().toString());
            rd.setOrder(param.getRouteOrder() == null ? SystemConfiguration.DEFAULT_ROUTE_CODE:param.getRouteOrder());
            if(StringUtils.isNotBlank(param.getFilters())) {
                rd.setFilters(JSONArray.parseArray(param.getFilters(), FilterDefinition.class));
            }
            if(StringUtils.isNotBlank(param.getPredicates())) {
                rd.setPredicates(JSONArray.parseArray(param.getPredicates(), PredicateDefinition.class));
            }
            return rd;
        }).collect(Collectors.toList()));
    }

    /* (非 Javadoc)
     * @param routeId
     * @return
     * @see org.springframework.cloud.gateway.route.RouteDefinitionWriter#delete(reactor.core.publisher.Mono)  
     * @exception
     */ 
    @Override
    public Mono<Void> delete(Mono<String> routeId) {
        log.info("删除路由Route:{}",routeId);
        return null;
    }

    /* (非 Javadoc)
     * @param route
     * @return
     * @see org.springframework.cloud.gateway.route.RouteDefinitionWriter#save(reactor.core.publisher.Mono)  
     * @exception
     */ 
    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {
        log.info("保存路由Route:{}",route.map(param->param.getUri()));
        return null;
    }

}
